﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BidFunLib.Services;
using log4net;
using BidFunLib.Entity;
using BidFunLib.BidWeb;


namespace BidFunLib.EntityHelper
{
    /// <summary>
    /// Object qui contient toutes les statistiques des ventes suivies en live.
    /// </summary>
    public class StatsBidSaleLiveManager
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(StatsBidSaleLiveManager));

        /// <summary>
        /// To calculate ponderate bid
        /// </summary>
        private Int32 bidNumber = 0;

        /// <summary>
        /// Une Map qui contient toutes les StatsBidSaleLiveMap 
        /// </summary>
        private Dictionary<String, StatsBidSaleLive> statsBidSaleLiveMap = null;

        /// <summary>
        /// Singleton instance
        /// </summary>
        private static StatsBidSaleLiveManager instance;

        /// <summary>
        /// Retreive singleton of this class
        /// </summary>
        public static StatsBidSaleLiveManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new StatsBidSaleLiveManager();
                }
                return StatsBidSaleLiveManager.instance;
            }
            set
            {
                StatsBidSaleLiveManager.instance = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private StatsBidSaleLiveManager()
        {
            statsBidSaleLiveMap = new Dictionary<String, StatsBidSaleLive>();
        }


        /// <summary>
        /// Cette méthode doit être appelée par l'ihm pour affecter les statistiques d'une vente 
        /// par une nouvelle enchère.
        /// 
        /// </summary>
        /// <param name="bidSale"></param>
        /// <param name="bidLogger"></param>
        /// <returns></returns>
        public StatsBidSaleLive updateStatsBidSaleLiveWithBidLogger(BidSale bidSale, BidLogger bidLogger)
        {
            // Firstly we try to retreive bidSale in local map (it is created if it doesn't exist.
            StatsBidSaleLive statsBidSaleLive = getStatsBidSaleLive(bidSale, bidLogger);

            // now we can update statistics with new logger
            statsBidSaleLive.updateStatsBidSaleLiveWithBidLogger(bidSale, bidLogger);

            // return the stats bidSale live all needed information for a live analysis of a sale are contained in this object.
            return statsBidSaleLive;
        }

        /// <summary>
        /// This method must be called when the bid sale has been terminated in order to remove
        /// the statsbidsale object from the local map.
        /// </summary>
        /// <param name="bidSale"></param>
        /// <param name="bidLogger"></param>
        /// <returns></returns>
        public Boolean endStatsBidSaleLive(BidSale bidSale)
        {
            if (!statsBidSaleLiveMap.ContainsKey(bidSale.BidFunIdentifier))
            {
                return statsBidSaleLiveMap.Remove(bidSale.BidFunIdentifier);
            }

            return false;
        }


        /// <summary>
        /// Cette methode 
        /// Elle doit être appelée a chaque fois qu'une nouvelle enchere est effectué
        /// 
        /// </summary>
        /// <param name="idBidSale"></param>
        /// <param name="?"></param>
        /// <param name="usersWithBid"></param>
        private StatsBidSaleLive getStatsBidSaleLive(BidSale bidSale, BidLogger bidLogger) 
        {
            StatsBidSaleLive statsBidSaleLive = null;

            // search for UserBidSaleDynamicData in dictionnary if doesn't exist we create it.
            if (!statsBidSaleLiveMap.ContainsKey(bidSale.BidFunIdentifier))
            {
                // we create new object : constructor will retreive all needed statistics data from DB.
                // this constructor call will retreive all object statistics    
                statsBidSaleLive = new StatsBidSaleLive(bidSale, bidLogger);

                // we add it to the Dictionary
                statsBidSaleLiveMap.Add(bidSale.BidFunIdentifier, statsBidSaleLive);
            }
            else
            {
                // we retreive the object in dictionary
                statsBidSaleLive = statsBidSaleLiveMap[bidSale.BidFunIdentifier];
            }

            // return the complete dictionnary that must be ordered
            return statsBidSaleLive;
        }

        /// <summary>
        /// Retourne la StatBidSaleLive seulement si elle existe en mémoire null sinon.
        /// </summary>
        /// <param name="bidSale"></param>
        /// <returns></returns>
        public StatsBidSaleLive getStatsBidSaleLiveOnlyIfExisting(BidSale bidSale)
        {
            StatsBidSaleLive statsBidSaleLive = null;

            // search for UserBidSaleDynamicData in dictionnary if doesn't exist we create it.
            if (statsBidSaleLiveMap.ContainsKey(bidSale.BidFunIdentifier))
            {
                // we retreive the object in dictionary
                statsBidSaleLive = statsBidSaleLiveMap[bidSale.BidFunIdentifier];
            }

            // return the complete dictionnary that must be ordered
            return statsBidSaleLive;
        }


    }
}
